﻿#region Imports
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using BulkRenamer.Util.Enums;
using CommonUtilities.Enums;
using CommonUtilities.Extensions;
using Microsoft.Office.Interop.Word;
#endregion

namespace BulkRenamer.Util
{
    /// <summary>
    /// Utilities used by other applications
    /// </summary>
    public static partial class BRUtilities
    {
        [DllImport("user32.dll")]
        private static extern int SetKeyboardState(byte[] keyState);

        [DllImport("user32.dll")]
        private static extern int GetKeyboardState(ref byte keyState);

        //http://answers.unity3d.com/questions/19112/how-to-get-and-set-the-state-of-numlock.html

        public static void SetKeyLock(eLockKey lockKey, Boolean isOn)
        {
            int NumLock = (int)lockKey;

            byte[] keyState = new byte[256];
            GetKeyboardState(ref keyState[0]);

            if (isOn) keyState[NumLock] = 1; else keyState[NumLock] = 0;
            SetKeyboardState(keyState);
        }

        #region Properties
        /// <summary>
        /// Properties to be copied from-to
        /// </summary>
        public static List<eCopyProperties> AllCopyProperties { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Copies design properties from one control to another
        /// </summary>
        /// <param name="reference">Control to refer from</param>
        /// <param name="target">Control to update</param>
        /// <param name="hasReferenceDropDown">TRUE: Control being referenced has a drop-down option
        /// <para>(reserves space to allow clicking on drop-down)</para></param>
        /// <param name="remove">Properties to remove</param>
        public static void CopyDesign(Control reference, Control target, bool hasReferenceDropDown,
           ObservableCollection<eCopyProperties> remove = null)
        {
            if (!AllCopyProperties.IsLinqIterable<eCopyProperties>(true)) PopulateProperties();
            if (remove.IsLinqIterable<eCopyProperties>(true)) RemoveProperties(remove);

            foreach (eCopyProperties copyProp in Enum.GetValues(typeof(eCopyProperties)).Cast<eCopyProperties>())
            {
                object propValue = reference.GetType().GetProperty(copyProp.ToString()).GetValue(reference, null);

                if (hasReferenceDropDown)
                {
                    switch (copyProp)
                    {
                        case eCopyProperties.Location:
                            Point pt = (Point)propValue;
                            pt.X += 3; pt.Y += 4;
                            propValue = pt;
                            break;
                        case eCopyProperties.Size:
                            Size sz = (Size)propValue;
                            sz.Width -= 23; sz.Height -= 1;
                            propValue = sz;
                            target.GetType().GetProperty(eCopyProperties.MinimumSize.ToString()).SetValue(target, propValue, null);
                            target.GetType().GetProperty(eCopyProperties.MaximumSize.ToString()).SetValue(target, propValue, null);
                            break;
                    }
                }

                target.GetType().GetProperty(copyProp.ToString()).SetValue(target, propValue, null);
            }
        }

        /// <summary>
        /// Populates properties to be copied
        /// </summary>
        private static void PopulateProperties()
        {
            List<eCopyProperties> allCopyProperties = new List<eCopyProperties>();
            foreach (eCopyProperties copyProp in Enum.GetValues(typeof(eCopyProperties)).Cast<eCopyProperties>())
                allCopyProperties.Add(copyProp);

            AllCopyProperties = allCopyProperties;
        }

        /// <summary>
        /// Removes the properties to be ignored
        /// </summary>
        /// <param name="remove"></param>
        private static void RemoveProperties(ObservableCollection<eCopyProperties> remove)
        {
            foreach (eCopyProperties copyProp in remove)
            {
                if (AllCopyProperties.Contains(copyProp)) AllCopyProperties.Remove(copyProp);
            }
        }
        #endregion
    }
}


/*
 * http://regexpal.com/
System.Collections.Generic.Dictionary<string, bool?> d = new System.Collections.Generic.Dictionary<string, bool?>();
d.Add("s0", null);
d.Add("s00", null);
d.Add("s110", null);
d.Add("s1e1", null);
d.Add("s01e1", null);
d.Add("s01e01", null);
d.Add("s1e01", null);
d.Add("s001e001", null);
d.Add("s00e011", null);
d.Add("`s00e011", null);
d.Add("`s00e01", null);
d._ForEach(x => d[x.Key] = x.Key.Length == 6 ? (bool?)BulkRenamer.Util.BRUtilities.CheckFileNameIndexPattern(x.Key) : null);
*/
